<template>
  <div class="employees">
    <el-card class="card-top">
      <div class="top">
        <div class="left">
          <i class="el-icon-info" />
          <span>共{{ total }}条记录</span>
        </div>
        <div class="right">
          <el-button
            size="mini"
            type="danger"
            @click="exportUser"
          >普通excel 导出</el-button>
          <el-button size="mini" type="info">复杂表头excel 导出</el-button>
          <el-button
            size="mini"
            type="success"
            @click="$router.push('/employees/import')"
          >excel导入</el-button>
          <el-button
            size="mini"
            type="primary"
            @click="addClick"
          >新增员工</el-button>
        </div>
      </div>
    </el-card>
    <el-card>
      <el-table border :data="list">
        <el-table-column type="index" label="序号" />
        <el-table-column prop="username" sortable label="姓名" />
        <el-table-column prop="staffPhoto" label="头像">
          <template v-slot="{ row }">
            <img
              v-imgerror
              class="avatar"
              :src="row.staffPhoto"
              alt=""
              @click="avatarClick(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column prop="mobile" sortable label="手机号" />
        <el-table-column prop="workNumber" sortable label="工号" />
        <el-table-column
          sortable
          :formatter="formatterType"
          prop="formOfEmployment"
          label="聘用形式"
        />
        <el-table-column sortable prop="departmentName" label="部门" />
        <el-table-column
          sortable
          :formatter="changeTime"
          prop="timeOfEntry"
          label="入职时间"
        />
        <el-table-column
          sortable
          :formatter="formatterEnableState"
          prop="enableState"
          label="状态"
        />
        <el-table-column prop="username" label="操作" width="300px">
          <template v-slot="{ row }">
            <el-button
              type="text"
              @click="$router.push('/employees/detail/' + row.id)"
            >查看</el-button>
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text">角色</el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          :page-sizes="[1, 2, 5, 10, 20]"
          :total="total"
          :current-page.sync="page.page"
          :page-size.sync="page.size"
          layout="total, sizes, prev,pager,next"
          @size-change="getdata"
          @current-change="getdata"
        />
      </div>
    </el-card>
    <!-- 新增弹框组件 -->
    <Add ref="add" />
    <qrcode ref="qrcodeCom" :is-show.sync="appShow" />
  </div>
</template>

<script>
import employeesData from '@/api/constant/employees.js'
import Add from './components/add'
import { export_json_to_excel } from '@/vendor/Export2Excel'
import { sysUser, sysUserDelete } from '@/api/employees'
import dayjs from 'dayjs'
import qrcode from '@/components/Qrcode/index.vue'
export default {
  components: {
    Add,
    qrcode
  },
  data() {
    return {
      list: [],
      page: {
        page: 1,
        size: 10
      },
      total: 20,
      appShow: false
    }
  },
  created() {
    this.getdata()
  },
  methods: {
    async getdata(row) {
      const { data: res } = await sysUser(this.page)
      this.list = res.rows
      this.total = res.total
    },
    // 通过table自带的格式化方法转换
    formatterType(row, column, cellValue, index) {
      // console.log(row, column, cellValue, index)
      const result = employeesData.hireType.find(
        (item) => item.id === cellValue
      )
      return result ? result.value : '-'
    },

    // 状态数据转换
    formatterEnableState(row, column, cellvalue, index) {
      const findItem = employeesData.workingState.find(
        (item) => +item.id === cellvalue
      )
      return findItem ? findItem.value : '-'
    },
    // 入职时间数据转换
    changeTime(row, column, cellValue, index) {
      return dayjs(cellValue).format('YYYY-MM-DD')
    },
    // 新增按钮点击
    addClick() {
      // 打开弹框
      this.$refs.add.isShow = true
    },
    // 删除点击事件
    del(id) {
      this.$confirm('您确定删除该员工吗？', '提示')
      this.$confirm('您确定删除该项吗？', '提示')
        .then(async() => {
          await sysUserDelete(id)
          // 提示
          this.$message.success('删除成功')
          // 刷新列表
          if (this.list.length === 1 && this.page.page > 1) {
            this.page.page--
          }
          this.getdata()
        })
        .catch(() => {})
    },
    async exportUser() {
      const res = await sysUser({
        page: 1,
        size: 9999
      })

      const data = res.data.rows.map((item) => {
        return [
          item.username,
          item.mobile,
          item.workNumber,
          this.formatterType(null, null, item.formOfEmployment),
          item.departmentName,
          this.changeTime(null, null, item.timeOfEntry),
          this.formatterEnableState(null, null, item.enableState)
        ]
      })
      export_json_to_excel({
        header: [
          '姓名',
          '手机号',
          '工号',
          '聘用形式',
          '部门',
          '入职时间',
          '状态'
        ],
        filename: '人资用户列表',
        data: data
      })
      this.$message.success('导出成功')
    },
    // 图片点击事件
    avatarClick(url) {
      console.log(url)
      this.appShow = true
      this.$refs.qrcodeCom.createCode(url)
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 20px;

  .card-top {
    margin-bottom: 30px;

    .top {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .left {
        margin-left: 20px;
        border: 1px solid #3faafc;
        background-color: #ebf6f9;
        padding: 6px;
        border-radius: 6px;
        i {
          color: #3faafc;
          padding-right: 8px;
        }
      }
    }
  }
  .avatar {
    width: 30px;
    height: 30px;
    object-fit: contain;
  }
}
</style>
